package calorie.com.calorie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import calorie.com.calorie.R;
import calorie.com.calorie.activity.AccelerateActivity;
import calorie.com.calorie.activity.ProfitRecordActivity;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.GetAssetsContact;
import calorie.com.calorie.contact.IncomeStastisticsContact;
import calorie.com.calorie.eventbus.EventImpl;
import calorie.com.calorie.response.AssertInfomationResponce;
import calorie.com.calorie.response.TodayProfitResponce;
import calorie.com.calorie.response.UserAssets;
import calorie.com.calorie.util.BLEUpdateBroadcastReceiver;
import calorie.com.calorie.util.NumberFormatUtils;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

/**
 * 收益统计
 */
public class IncomeStastisticsFragment extends MvpFragment<IncomeStastisticsContact.IncomeStastisticsPresent> implements IncomeStastisticsContact.IIncomeStastisticsView, GetAssetsContact.IgetAssets {
    @BindView(R.id.tv_banlance_cal_value)
    TextView tvBanlanceCalValue;
    @BindView(R.id.tv_banlance_vit_value)
    TextView tvBanlanceVitValue;
    @BindView(R.id.tv_today_income_value)
    TextView tvTodayIncomeValue;
    @BindView(R.id.tv_accelerate)
    TextView tvAccelerate;
    @BindView(R.id.tv_candy_reward)
    TextView tvCandyReward;
    @BindView(R.id.tv_sport_caculate)
    TextView tvSportCaculate;
    @BindView(R.id.tv_manager_reward)
    TextView tvManagerReward;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.tv_profi_record)
    TextView tvProfiRecord;
    @BindView(R.id.tv_jiasu)
    TextView tvJiasu;
    Unbinder unbinder;
    private GetAssetsContact.GetAssetsPresent assetsPresent;

    @Override
    public void initBeforView(Bundle savedInstanceState) {
        super.initBeforView(savedInstanceState);
        assetsPresent = new GetAssetsContact.GetAssetsPresent();
        assetsPresent.attach(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresent.getTodayProfit(context);

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                assetsPresent.getAssets(context);
            }
        });
    }

    private void setAssertMsg() {
        if (StaticValue.userAssets != null) {
            tvBanlanceCalValue.setText(NumberFormatUtils.keepTwoDecimal(StaticValue.userAssets.getCalNum()));

//            tvBanlanceVitValue.setText(NumberFormatUtils.keepTwoDecimal(StaticValue.userAssets.getTivNum()));//今日收益

//            BLEUpdateBroadcastReceiver.vit_num_view.add(tvBanlanceVitValue);
            BLEUpdateBroadcastReceiver.cal_num_view.add(tvBanlanceCalValue);

            tvJiasu.setText(StaticValue.userAssets.getSpeed() + "倍");
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_income_stastistics;
    }


    @OnClick({R.id.tv_accelerate, R.id.tv_profi_record})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_accelerate:
                startActivity(new Intent(context, AccelerateActivity.class));
                break;
            case R.id.tv_profi_record:
                startActivity(new Intent(context, ProfitRecordActivity.class));
                break;
        }
    }

    @Override
    public void getTodayProfitData(Response<TodayProfitResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            TodayProfitResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;
            tvBanlanceVitValue.setText(data.getTodayProfit() + "");
            /*tvTodayIncomeValue.setText(data.getTodayProfit()+"");*/
            tvSportCaculate.setText(data.getSportProfit() + "CAL");
            tvCandyReward.setText(data.getCandyProfit() + "CAL");
            tvManagerReward.setText(data.getManageProfit() + "CAL");
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected IncomeStastisticsContact.IncomeStastisticsPresent createPresent() {
        return new IncomeStastisticsContact.IncomeStastisticsPresent();
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveBuyAccelerate(EventImpl.BuyAccelerateEvent event) {
        if (!ObjectUtils.isEmpty(event)) {
            setAssertMsg();
        }
    }

    @Override
    public void getAssetsResult(Response<AssertInfomationResponce> response) {
        refresh.finishRefresh();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            Gson gs = new Gson();
            StaticValue.userAssets = gs.fromJson(gs.toJson(response.body().getData()), UserAssets.class);//保存用户资产信息

            AssertInfomationResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;
            SPUtils.putString(Constant.PURSE_ADDRESS, data.getMoneyAddress(), context);
            tvBanlanceCalValue.setText(NumberFormatUtils.keepTwoDecimal(data.getCalNum()));
            tvBanlanceVitValue.setText(NumberFormatUtils.keepTwoDecimal(data.getTivNum()));
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        setAssertMsg();
    }


}
